
ResApplyEdit = new Pearlsen.BaseEdit({
	formId:'resApplyForm',
	idProperty:'applyId',
	moduleName:'ResApply',
	queryHandler:'ResApplyQueryHandler',
	queryMethod:'getEntity',
	dataSetHandler:'ResApplyDataSetHandler',
	saveMethod:'save',
	addHostConfig:function(){
		var hostConfigWin = Ext.getCmp("hostConfigWin");
		hostConfigWin.setTitle("添加主机"  );
		hostConfigWin.show();
	},
	renderGrid:function(value,metaData,record,rowIndex,colIndex,store,view){
		var ret = '' ,memoryunit,storageunit,opsSeries;	
		var units =['MB','GB','TB'];
		//var ops=[['XP_TPL','Window XP'],['WIN7_TPL','Window 7'],['LINUX_TPL','LINUX']];
		var ops={XP_TPL:'Window XP',WIN7_TPL:'Window 7',LINUX_TPL:'LINUX'};
		opsSeries = record.data['opsSeries'];
		memoryunit = record.data['memoryunit'];
		storageunit = record.data['storageunit'];
		switch(colIndex){
			case 3:
				ret =ops[value];
				break;
			case 5:
				ret = value+units[memoryunit];
				break;
			case 6:
				ret = value +units[storageunit];
				break;
		}
		return ret;
	},
	
	addHost:function(){
		var hostForm = Ext.getCmp("hostForm");
		var hostGridStore = Ext.getCmp("hostConfigGrid").getStore();
		if(hostForm.getForm().isValid()){
			var hostObj={};
			//configId hostName opsSeries opsName cups netcards 
			//memory fmemoryunit storage	fstorageunit	hostDesc
  			hostObj["configId"]=Ext.getCmp("configId").getValue();
  			hostObj["applyId"]=Ext.getCmp("applyId").getValue();
  			hostObj["hostDesc"]=Ext.getCmp("hostDesc").getValue();
  			hostObj["opsSeries"]=Ext.getCmp("opsSeries").getValue();
  			hostObj["opsName"]=Ext.getCmp("opsName").getValue();
  			hostObj["cups"]=Ext.getCmp("cups").getValue()+"";
  			hostObj["memory"]=Ext.getCmp("memory").getValue()+"";
  			hostObj["memoryunit"]=Ext.getCmp("fmemoryunit").getValue();
  			hostObj["storage"]=Ext.getCmp("storage").getValue()+"";
  			hostObj["storageunit"]=Ext.getCmp("fstorageunit").getValue();
  			hostObj["netcards"]=Ext.getCmp("netcards").getValue()+"";
  			hostGridStore.add(hostObj);
		}
		Ext.getCmp("hostConfigWin").hide();
	},
	removeHost:function(){
		var hostGridStore = Ext.getCmp("hostConfigGrid").getStore();
		var records = Ext.getCmp("hostConfigGrid").getSelectionModel().getSelection();
		hostGridStore.remove(records);
	},
	closeHostWin:function(){
		Ext.getCmp("hostConfigWin").hide();
	},

	getData:function(){
		//applyId resName runDays storageDays resDesc
		var applyObj ={};
		applyObj["applyId"]=Ext.getCmp("applyId").getValue();
		applyObj["resName"]=Ext.getCmp("resName").getValue();
		applyObj["runDays"]=Ext.getCmp("runDays").getValue();
		applyObj["storageDays"]=Ext.getCmp("storageDays").getValue();
		applyObj["resDesc"]=Ext.getCmp("resDesc").getValue();
	},
	getHostList:function(){
		var data = [];
		var hostGridStore = Ext.getCmp("hostConfigGrid").getStore();
		hostGridStore.each(function(record){
			data.push(record.data);
		});
		return data;
	},
	submitForm:function(){
		if(this.getForm().isValid()){
			var me = this;
	    	var fields = this.getForm().getValues();
	    	var dataSet = new DataSet(this.dataSetHandler,"_save_dataset");
	    	dataSet.setHandlerMethod(this.saveMethod);
	    	dataSet.newRecord();
	    	Ext.iterate(fields,function(name,value){
	    		dataSet.setValue(name,value);
	    	});
	    	dataSet.setValue("hostList",me.getHostList());
	    	dataSet.execute(function(result){
	    		if(result.getStatus()=="200"){
	    			Pearlsen.Utils.showSuccessMsg('保存成功');
	    			GlobalEvent.fireEvent(me.moduleName+"_refresh_list");
	    			me.close();
	    			return;
	    		}else{
	    			Pearlsen.Utils.showErrorMsg('保存失败,原因:'+result.message);
	    			return ;
	    		}
	    	});	
		}
	},
	submitApply:function(){
		var me = this;

		if(this.getForm().isValid()){
			var hostList = me.getHostList();
			if( Ext.isEmpty(hostList)){
				Pearlsen.Utils.showErrorMsg('请填写要申请的主机');
				return ;
			}
			
	        Ext.MessageBox.confirm('提示', '您确定要提交申请吗?',function(btn){
	        	if(btn=='yes'){
	        		
	    	    	var fields = me.getForm().getValues();
	    	    	var dataSet = new DataSet(me.dataSetHandler,"_save_dataset");
	    	    	dataSet.setHandlerMethod("submitApply");
	    	    	dataSet.newRecord();
	    	    	Ext.iterate(fields,function(name,value){
	    	    		dataSet.setValue(name,value);
	    	    	});
	    	    	dataSet.setValue("hostList",hostList);
	    	    	dataSet.execute(function(result){
	    	    		if(result.getStatus()=="200"){
	    	    			if( result.getRecords() && result.getRecords().length > 0 ){
	    	    				var record = result.getRecords()[0];
	    	    				var applystatus = record['applystatus'];
	    	    				var applyError = record['applyError'];
	    	    				if( applystatus =='1'){
			    	    			Pearlsen.Utils.showSuccessMsg('提交成功');
			    	    			GlobalEvent.fireEvent(me.moduleName+"_refresh_list");
			    	    			me.close();
	    	    				}else {
		    					      Ext.Msg.show({
		    						           title:'提交失败',
		    						           msg: applyError,
		    						           buttons: Ext.Msg.YES,
		    						           icon: Ext.Msg.INFO
		    						   });
	    	    				}
	    	    			}
	    	    			return;
	    	    		}else{
	    	    			Pearlsen.Utils.showErrorMsg('提交失败,原因:'+result.message);
	    	    			return ;
	    	    		}
	    	    	});		        		
	        		
	        		
	        	}
	        });
	    	
		}		
	},
	loadForm:function(){
		 var query = new Query(this.queryHandler,"_load_dataset");
		 query.setHandlerMethod(this.queryMethod);		 
		 query.setURL('/query.asp');
		 query.addCondition("objId", this.objId);
		 query.setCompletedFun(this.afterloadForm);
		 query.setCompletedFunScope(this);
		 query.load();
	},
	afterloadForm:function(success,dataset,response, opts){
		if(success){
			if( dataset && !Ext.isEmpty(dataset.record)){
				var record = dataset.record[0];
				this.getForm().setValues(record);
				var hostGridStore = Ext.getCmp("hostConfigGrid").getStore();
				if(	Ext.isArray(record.hostList) ){
					hostGridStore.loadData(record.hostList);
				}
			}
		}else{
			
		}
	},
	loadAuditLog:function(){
		var auditLogStore = Ext.getCmp("auditLogGrid").getStore();
  		var condition=new Condition("applyId",this.objId);
  		auditLogStore.clearCondition();
  		auditLogStore.addCondition(condition);
  		auditLogStore.load();
	},
	renderAuditGrid:function(value,metaData,record,rowIndex,colIndex,store,view){
		var status =['审批不通过','审批通过','执行完成','执行失败'];
		return status[value];
	},
	/**
	 * 资源使用情况查询
	 */
	queryRes:function(){
		var startDate = Ext.getCmp("startDate").getRawValue();
		var endDate = Ext.getCmp("endDate").getRawValue();
		if( !Ext.isEmpty(startDate) && !Ext.isEmpty(endDate)  ){
			var win = Ext.getCmp("resqueyWin");
			win.show();
			var url = Pls.Webpath +"/pages/rms/resgantt/ResGantt.jsp?startDate={0}&endDate={1}";
			url = url.format(startDate,endDate);
			document.getElementById("framegtt").src=url;
		}
	}
	
});